//
//  XJ_ApplicateCashViewController.m
//  XunJieProject
//
//  Created by xue on 2018/6/10.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_ApplicateCashViewController.h"
#import "XJ_ApplicateRecordViewController.h"
#import "XJ_ApplayResultViewController.h"

@interface XJ_ApplicateCashViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *applyCashBtn;
@property (weak, nonatomic) IBOutlet UIImageView *bankLogoImage;
@property (weak, nonatomic) IBOutlet ZDXXibTextField *cardNoTailTF;
@property (weak, nonatomic) IBOutlet UILabel *bankLabel;
@property (weak, nonatomic) IBOutlet ZDXXibLabel *balanceLabel;
@property (weak, nonatomic) IBOutlet UITextField *tixianTF;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *up_scrollView;
@property (assign, nonatomic) BOOL isHaveDian;

@end

@implementation XJ_ApplicateCashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.bankLabel.text =_userInfoModel.depositBank;
    [self.bankLogoImage sd_setImageWithURL:[NSURL URLWithString:_userInfoModel.depositBankLogo] placeholderImage:[UIImage imageNamed:@"img_no_image"]];
    if (_userInfoModel.depositCardNo.length>4) {
        NSString *cardNoTail =[_userInfoModel.depositCardNo substringFromIndex:_userInfoModel.depositCardNo.length- 4];
        self.cardNoTailTF.text = [NSString stringWithFormat:@"尾号%@",cardNoTail];
    }else{
        self.cardNoTailTF.text =[NSString stringWithFormat:@"尾号%@",_userInfoModel.depositCardNo];;

    }
    
    self.balanceLabel.text =[NSString stringWithFormat:@"可用余额￥%@元",_userInfoModel.balance];
    
}
-(void)creatMainUI{
    
    self.nav_titleLabel.text =@"提现申请";
    self.nav_rightButton.hidden =NO;
    [self.nav_rightButton setTitle:@"记录" forState:UIControlStateNormal];
    [self.nav_rightButton addTarget:self action:@selector(Click_right) forControlEvents:UIControlEventTouchUpInside];
    
    _applyCashBtn.backgroundColor =Color_ZhuSeDiao;
    _applyCashBtn.layer.cornerRadius =4;
    _applyCashBtn.layer.masksToBounds =YES;
    _up_scrollView.constant =SafeAreaTopHeight;
    _tixianTF.delegate =self;
    _tixianTF.keyboardType =UIKeyboardTypeDecimalPad;
    
}
#pragma mark--记录
-(void)Click_right{
    XJ_ApplicateRecordViewController *record =[[XJ_ApplicateRecordViewController alloc]init];
    [self.navigationController pushViewController:record animated:YES];
}

- (IBAction)Click_btn:(id)sender {
    
    UIButton *button =(UIButton*)sender;
    switch (button.tag) {
        case 0://全部提现
        {
            if (_userInfoModel.balance.length==0||[_userInfoModel.balance isEqualToString:@"0.00"]) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"余额不足" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            self.tixianTF.text =_userInfoModel.balance;
        }
            break;
        case 1://申请提现
        {
            if (_tixianTF.text.length==0) {
                ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"请输入提现金额" image:@"pop_msg_tip_xs"];
                [alext showXLAlertView];
                return;
            }
            
            
            [self loadMoneyWithDrawal];
        }
            break;
            
        default:
            break;
    }
}
#pragma mark--提现接口
-(void)loadMoneyWithDrawal{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:USERID forKey:@"userId"];
    [dic setValue:token forKey:@"token"];
    
    [dic setValue:_tixianTF.text forKey:@"drawalMoney"];
    [XJ_Utils signPost:@"/money/withDrawal?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        
//        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        
        XJ_ApplayResultViewController *result =[[XJ_ApplayResultViewController alloc]init];
        result.resultType =@"0";
        result.ValueStr =self.tixianTF.text;
        [self.navigationController pushViewController:result animated:YES];
       
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}


/**
 *  textField的代理方法，监听textField的文字改变
 *  textField.text是当前输入字符之前的textField中的text
 *
 *  @param textField textField
 *  @param range     当前光标的位置
 *  @param string    当前输入的字符
 *
 *  @return 是否允许改变
 */
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    /*
     * 不能输入.0-9以外的字符。
     * 设置输入框输入的内容格式
     * 只能有一个小数点
     * 小数点后最多能输入两位
     * 如果第一位是.则前面加上0.
     * 如果第一位是0则后面必须输入点，否则不能输入。
     */
    
    // 判断是否有小数点
    if ([textField.text containsString:@"."]) {
        self.isHaveDian = YES;
    }else{
        self.isHaveDian = NO;
    }
    
    if (string.length > 0) {
        
        //当前输入的字符
        unichar single = [string characterAtIndex:0];
        NSLog(@"single = %c",single);
        
        // 不能输入.0-9以外的字符
        if (!((single >= '0' && single <= '9') || single == '.'))
        {
//            [MBProgressHUD bwm_showTitle:@"您的输入格式不正确" toView:self hideAfter:1.0];
            return NO;
        }
        
        // 只能有一个小数点
        if (self.isHaveDian && single == '.') {
//            [MBProgressHUD bwm_showTitle:@"最多只能输入一个小数点" toView:self hideAfter:1.0];
            return NO;
        }
        
        // 如果第一位是.则前面加上0.
        if ((textField.text.length == 0) && (single == '.')) {
            textField.text = @"0";
        }
        
        // 如果第一位是0则后面必须输入点，否则不能输入。
        if ([textField.text hasPrefix:@"0"]) {
            if (textField.text.length > 1) {
                NSString *secondStr = [textField.text substringWithRange:NSMakeRange(1, 1)];
                if (![secondStr isEqualToString:@"."]) {
//                    [MBProgressHUD bwm_showTitle:@"第二个字符需要是小数点" toView:self hideAfter:1.0];
                    return NO;
                }
            }else{
                if (![string isEqualToString:@"."]) {
//                    [MBProgressHUD bwm_showTitle:@"第二个字符需要是小数点" toView:self hideAfter:1.0];
                    return NO;
                }
            }
        }
        
        // 小数点后最多能输入两位
        if (self.isHaveDian) {
            NSRange ran = [textField.text rangeOfString:@"."];
            // 由于range.location是NSUInteger类型的，所以这里不能通过(range.location - ran.location)>2来判断
            if (range.location > ran.location) {
                if ([textField.text pathExtension].length > 1) {
//                    [MBProgressHUD bwm_showTitle:@"小数点后最多有两位小数" toView:self hideAfter:1.0];
                    return NO;
                }
            }
        }
        
    }
    
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
